local function read_files(fileName) --读取文件
    local f = assert(io.open(fileName, "r"))
    --[[
    读取所有文件内容 *all
    读取一行 *line
    读取一个数字 *number
    读取一个不超过num个数的字符串 <num>
    ]]
    -- local content = f:read("*all"))
    local filetab = {}
    local line = f:read("*line")
    -- local content = f:read("*all")
    while line do
        table.insert(filetab, line)
        line = f:read("*line")
    end
    f:close() --紧跟关闭

    return filetab
end

local function write_files(fileName, filelist) --按行写入文件
    local f = assert(io.open(fileName, "w"))
    for i, line in ipairs(filelist) do
        if #line == 0 then --若数组中没有元素，则退出
            break
        end
        f:write(line)
        f:write("\n")
    end
end

local function n_input() --判断合法输入，直到输入合法时输出
    print("\n--------------------Menu--------------------\n")
    print("To choose your choice, please input the number of each choice.")
    print("Choice 1: view all saved data")
    print("Choice 2: save an item data")
    print("Choice 3: read an item data")
    print("Choice 4: delete an item data")
    local m = io.read()
    while m ~= "1" and m ~= "2" and m ~= "3" and m ~= "4" do --直到输入正确才退出循环
        print("-----Input Error!-----")
        print("Please input again.")
        m = io.read()
    end
    return m
end

local function view_list(filelist) --choice1，View函数
    print("----------View----------")
    print("You choose to view all data")
    local length = #filelist
    if length == 0 then --数组元素数量为0
        print("There is no saved data")
    else
        print("There are(is) " .. length / 2 .. " data.") --数组元素不为0
        for i, line in ipairs(filelist) do
            if i % 2 == 1 then
                io.write("No.", (i + 1) / 2, " The price of a(an) ", line, " is ") --名字
            else
                print(line) --价格
            end
        end
    end
    print("You can input 0 to return to the main menu.")
    local m = io.read()
    while m ~= "0" do
        print("-----Input Error-----")
        print("Please input again.")
        print("You can input 0 to return to the main menu.")
        m = io.read()
    end
end

local function save_list(filelist) --choice2，save函数
    print("----------Save----------")
    print("You choose to save data\nNow please input the name of an item:")
    local name = io.read()
    print("Now please input the price of this item:")
    local price = io.read()
    table.insert(filelist, name) --插入到数组中
    table.insert(filelist, price)
    print("You have saved the data: The price of a(an) " .. name .. " is " .. price)
    print("You can input 0 to return to the main menu.")
    local m = io.read()
    while m ~= "0" do
        print("-----Input Error-----")
        print("Please input again.")
        print("You can input 0 to return to the main menu.")
        m = io.read()
    end
    return filelist
end

local function read_list(filelist)
    print("----------Read----------")
    print("You choose to read data")
    print("Please input the name of the data which you want to read:")
    local name = io.read()
    local flag = 0
    local length = #filelist
    for i, line in ipairs(filelist) do
        if line == name then --找到对应的元素位置
            print("The price of a(an) " .. line .. " is " .. filelist[i + 1])
            flag = 1
            break
        end
        if i == length then
            break
        end
    end
    if flag == 0 then --数组中没有该元素的情况
        print("There is no data of " .. name)
    end
    print("You can input 0 to return to the main menu.")
    local m = io.read()
    while m ~= "0" do
        print("-----Input Error-----")
        print("Please input again.")
        print("You can input 0 to return to the main menu.")
        m = io.read()
    end
end

local function delete_list(filelist)
    print("----------Delete----------")
    print("You choose to delete a data")
    print("Please input the name of the data which you want to delete:")
    local name = io.read()
    local flag = 0
    local length = #filelist
    for i, line in ipairs(filelist) do
        if line == name then --找到对应元素的位置
            filelist[i] = nil
            filelist[i + 1] = nil
            flag = 1
            print("You delete the data of " .. name)
            break
        end
        if i == length then
            break
        end
    end
    local filelist1 = {}
    for i = 1, length do --将元素复制到新数组中
        if filelist[i] ~= nil then
            table.insert(filelist1, filelist[i])
        end
    end
    if flag == 0 then --数组中没有该元素的情况
        print("There is no data of " .. name)
    end
    print("You can input 0 to return to the main menu.")
    local m = io.read()
    while m ~= "0" do
        print("-----Input Error-----")
        print("Please input again.")
        print("You can input 0 to return to the main menu.")
        m = io.read()
    end
    return filelist1
end

local result = read_files("task2/test.txt")
local t = 10
print("Welcome, please choose your choice")
while t > 0 do
    local n = n_input()
    if n == "1" then
        view_list(result)
    elseif n == "2" then
        result = save_list(result)
    elseif n == "3" then
        read_list(result)
    else
        result = delete_list(result)
    end
    t = t - 1
end

write_files("task2/test.txt", result)
